package com.brillilab.service.core.hardware.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.hardware.HardwareOperateRecordMapper;
import com.brillilab.domain.po.hardware.HardWareOperateRecord;
import com.brillilab.domain.po.space.Device;
import com.brillilab.domain.vo.hardware.HardWareOperateRecordVo;
import com.brillilab.service.core.hardware.IHardwareOperateRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: HardwareOperateRecordServiceImpl
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/1/2910:44
 */
@Service
@Transactional
public class HardwareOperateRecordServiceImpl implements IHardwareOperateRecordService {
    @Resource
    private HardwareOperateRecordMapper recordMapper;
    @Override
    public boolean updateRecord(HardWareOperateRecordVo record) {
        LambdaQueryWrapper<HardWareOperateRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HardWareOperateRecord::getAioId,record.getAioId());
        queryWrapper.in(HardWareOperateRecord::getDeviceId,record.getDeviceIdList());
        List<HardWareOperateRecord> dbRecordList = recordMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(dbRecordList)){
            for(int i=0;i<record.getDeviceIdList().size();i++){
                HardWareOperateRecord r = new HardWareOperateRecord();
                BeanUtils.copyProperties(record,r);
                r.setDeviceId(record.getDeviceIdList().get(i));
                recordMapper.insert(r);
            }
        }else{
            for(int i=0;i<record.getDeviceIdList().size();i++){
                Long deviceId= record.getDeviceIdList().get(i);
                HardWareOperateRecord r = new HardWareOperateRecord();
                 HardWareOperateRecord db =  dbRecordList.stream().filter(dr->dr.getDeviceId().equals(deviceId)).findFirst().get();
                BeanUtils.copyProperties(record,r);
                r.setDeviceId(record.getDeviceIdList().get(i));
                r.setId(db.getId());
                recordMapper.updateById(r);
            }
        }
        return true;
    }

    @Override
    public List<HardWareOperateRecord> list(Long aioId) {
        LambdaQueryWrapper<HardWareOperateRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HardWareOperateRecord::getAioId,aioId);
        return recordMapper.selectList(queryWrapper);
    }

    @Override
    public HardWareOperateRecord selectByDeviceId(Long deviceId) {
        return recordMapper.selectOne(
                new LambdaQueryWrapper<HardWareOperateRecord>()
                .eq(HardWareOperateRecord::getDeviceId,deviceId));
    }

    @Override
    public HardWareOperateRecord selectLockByDeviceId(Long deviceId) {
        return recordMapper.selectOne(
                new LambdaQueryWrapper<HardWareOperateRecord>()
                        .eq(HardWareOperateRecord::getDeviceId,deviceId)
                        .isNotNull(HardWareOperateRecord::getLockerId));
    }

    @Override
    public List<HardWareOperateRecord> selectLockListByDeviceIds(List<Long> deviceIds) {
        return recordMapper.selectList(
                new LambdaQueryWrapper<HardWareOperateRecord>()
                        .in(HardWareOperateRecord::getDeviceId,deviceIds)
                        .isNotNull(HardWareOperateRecord::getLockerId));
    }
}
